﻿using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using DAT = CPPEI.ETL.Domain.Data;

namespace CPPEI.ETL.WebApi.DtSer.ExtDS
{
    /// <summary>
    /// 上传扩展服务程序集
    /// </summary>
    [CPPEI.WebApi.AuthPostAction("ExtDS", "Upload", "1.0")]
    public class UploadAction : BaseAction<UploadParams, UploadResult>
    {
        protected override ActionResult<UploadResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            UploadParams opParams)
        {
            Guid tempId = Guid.NewGuid();

            string assemblyFolder = string.Format("{0}ExtDS\\Temp\\", System.Configuration.ConfigurationManager.AppSettings["AssemblyFolder"]);

            string filePath = string.Format("{0}{1}.dll", assemblyFolder, tempId.ToString("N"));

            FileInfo fileInfo = new FileInfo(filePath);
            if(fileInfo.Exists)
            {
                // 存在同名文件不上传
                return new ActionResult<UploadResult>(false, 8, "已存在同名文件", null);
            }

            if (!fileInfo.Directory.Exists)
            {
                fileInfo.Directory.Create();
            }
            opParams.File.SaveAs(filePath);

            Logger.DebugToTag(Library.LogTAG, string.Format("上传扩展服务程序集:{0}", filePath));

            SER.ExtDSService service = new SER.ExtDSService();
            ServiceResult<DAT.ExtDSAssembly> serResult = service.Parse(File.ReadAllBytes(filePath));

            if(serResult.IsSuccess)
            {
                UploadResult result = new UploadResult();
                result.Id = tempId;
                result.Assembly = serResult.Data.Assembly;
                result.Version = serResult.Data.Version;
                for (int index = 0; index < serResult.Data.Types.Count; index++)
                {
                    UploadResult.Type item = new UploadResult.Type();
                    item.Id = serResult.Data.Types[index].Id;

                    if (!string.IsNullOrEmpty(serResult.Data.Types[index].Description))
                    {
                        item.Description = serResult.Data.Types[index].Description;
                    }
                    else
                    {
                        item.Description = string.Empty;
                    }

                    if (!string.IsNullOrEmpty(serResult.Data.Types[index].Name))
                    {
                        item.Name = serResult.Data.Types[index].Name;
                    }
                    else
                    {
                        item.Name = string.Empty;
                    }

                    result.Types.Add(item);
                }
                return new ActionResult<UploadResult>(result);
            }
            else
            {
                return new ActionResult<UploadResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class UploadParams
    {
        public System.Web.HttpPostedFile File { get; set; }
    }

    public class UploadResult
    {
        public Guid Id { get; set; }
        public string Assembly { get; set; }

        public string Version { get; set; }

        /// <summary>
        /// 程序集所在的相对路径
        /// </summary>
        public string FilePath { get; set; }

        public List<Type> Types = new List<Type>();

        public class Type
        {
            public Guid Id { get; set; }

            public string Name { get; set; }

            public string Description { get; set; }
        }
    }
}
